package com.example.dinner;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private ViewPager mainVp;
    private RadioGroup tabBar;
    private RadioButton tabHome;
    private RadioButton tabList;
    private RadioButton tabSetting;
    private FragmentDinner fragmentDinner;
    private FragmentList fragmentList;
    private FragmentSetting fragmentSetting;
    private ArrayList<Fragment> fragments;
    private ArrayList<String> titles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initViewPager();
    }

    private void initViewPager() {

        fragmentDinner = new FragmentDinner();
        fragmentList = new FragmentList();
        fragmentSetting = new FragmentSetting();
        fragments = new ArrayList<>();
        titles = new ArrayList<>();
        fragments.add(fragmentDinner);
        fragments.add(fragmentList);
        fragments.add(fragmentSetting);

        FPAdater fpAdater = new FPAdater(getSupportFragmentManager(), fragments, titles);

        mainVp.setAdapter(fpAdater);
        mainVp.setOnPageChangeListener(this);

    }

    private void initView() {
        mainVp = (ViewPager) findViewById(R.id.main_vp);
        tabBar = (RadioGroup) findViewById(R.id.tab_bar);
        tabHome = (RadioButton) findViewById(R.id.tab_home);
        tabList = (RadioButton) findViewById(R.id.tab_list);
        tabSetting = (RadioButton) findViewById(R.id.tab_setting);
        tabHome.setOnClickListener(this);
        tabList.setOnClickListener(this);
        tabSetting.setOnClickListener(this);
        tabHome.setChecked(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tab_home:
                tabHome.setChecked(true);
                mainVp.setCurrentItem(0);
                break;
            case R.id.tab_list:
                tabList.setChecked(true);
                mainVp.setCurrentItem(1);

                break;
            case R.id.tab_setting:
                tabSetting.setChecked(true);
                mainVp.setCurrentItem(2);

                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        switch (position) {
            case 0:
                tabHome.setChecked(true);
                break;
            case 1:
                tabList.setChecked(true);
                break;
            case 2:
                tabSetting.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}